package com.jhf.skypac_pda;

import android.annotation.SuppressLint;
import android.app.Application;
import android.bld.ScanManager;
import android.content.Context;
import android.os.Build;

import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.flattener.ClassicFlattener;
import com.elvishew.xlog.formatter.message.json.DefaultJsonFormatter;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.Printer;
import com.elvishew.xlog.printer.file.FilePrinter;
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy2;
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy;
import com.example.iscandemo.iScanInterface;
import com.jhf.skypac_pda.model.CabDetailModel;
import com.jhf.skypac_pda.model.CabinetListMpdel;
import com.jhf.skypac_pda.model.LoginCabModel;
import com.jhf.skypac_pda.model.SiteDispatchModel;
import com.jhf.skypac_pda.model.loginModel;
import com.jhf.skypac_pda.xlog.StampFileNameGenerator;
import com.jhf.skypac_pda.xlog.Utf8Writer;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;

import okgo.OkGo;

public class HxApplication extends Application {
    private static HxApplication mBaseApplication = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mBaseApplication = this;
        initXlog();
        OkGo.getInstance().init(this);


        //第三个参数为SDK调试模式开关，调试模式的行为特性如下：
        //
        //输出详细的Bugly SDK的Log；
        //每一条Crash都会被立即上报；
        //自定义日志将会在Logcat中输出。
        //建议在测试阶段建议设置成true，发布时设置为false。
        CrashReport.initCrashReport(getApplicationContext(), "abcaad90f5", true);

    }

    public static HxApplication getIntance() {
        if (null == mBaseApplication) {
            mBaseApplication = new HxApplication();
        }
        return mBaseApplication;
    }

    private void initXlog() {
        LogConfiguration config = new LogConfiguration.Builder().logLevel(LogLevel.ALL)           // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                .tag("X-LOG")                   // Specify TAG, default: "X-LOG"
                // .enableThreadInfo()                                 // Enable thread info, disabled by default
                // .enableStackTrace(2)                                // Enable stack trace info with depth 2, disabled by default
                // .enableBorder()                                     // Enable border, disabled by default
                .jsonFormatter(new DefaultJsonFormatter())               // Default: DefaultJsonFormatter
                // .xmlFormatter(new MyXmlFormatter())                 // Default: DefaultXmlFormatter
                // .throwableFormatter(new MyThrowableFormatter())     // Default: DefaultThrowableFormatter
                // .threadFormatter(new MyThreadFormatter())           // Default: DefaultThreadFormatter
                // .stackTraceFormatter(new MyStackTraceFormatter())   // Default: DefaultStackTraceFormatter
                // .borderFormatter(new MyBoardFormatter())            // Default: DefaultBorderFormatter
                // .addObjectFormatter(AnyClass.class,                 // Add formatter for specific class of object
                //     new AnyClassObjectFormatter())                  // Use Object.toString() by default
//                .addInterceptor(new BlacklistTagsFilterInterceptor(    // Add blacklist tags filter
//                        "blacklist1", "blacklist2", "blacklist3"))
                // .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter
                //     "whitelist1", "whitelist2", "whitelist3"))
                // .addInterceptor(new MyInterceptor())                // Add a log interceptor
                .build();

        Printer androidPrinter = new AndroidPrinter();             // Printer that print the log using android.util.Log
        Printer filePrinter = new FilePrinter                      // Printer that print the log to the file system
                .Builder(new File(getExternalCacheDir().getAbsolutePath(), "log").getPath())       // Specify the path to save log file
                .fileNameGenerator(new StampFileNameGenerator())        // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(new FileSizeBackupStrategy2(1024 * 1024 * 10L, 10))             // Default: FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy(new FileLastModifiedCleanStrategy(30 * 24 * 60 * 60 * 1000L))
                .flattener(new ClassicFlattener())                     // Default: DefaultFlattener
                .writer(new Utf8Writer() {                           // Default: SimpleWriter
                    @Override
                    public void onNewFileCreated(File file) {
                        super.onNewFileCreated(file);
                        final String header = "\n>>>>>>>>>>>>>>>> File Header >>>>>>>>>>>>>>>>" + "\nDevice Manufacturer: " + Build.MANUFACTURER + "\nDevice Model       : " + Build.MODEL + "\nAndroid Version    : " + Build.VERSION.RELEASE + "\nAndroid SDK        : " + Build.VERSION.SDK_INT + "\nApp VersionName    : " + BuildConfig.VERSION_NAME + "\nApp VersionCode    : " + BuildConfig.VERSION_CODE + "\n<<<<<<<<<<<<<<<< File Header <<<<<<<<<<<<<<<<\n\n";
                        appendLog(header);
                    }
                }).build();

        XLog.init(                                                 // Initialize XLog
                config,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
                androidPrinter,                                        // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
                filePrinter);
    }

    /*private void initLanChangPda() {
        try {
            ScanManager scanManager = ScanManager.getDefaultInstance(this);
            if (null != scanManager) {
                // 广播模式
                scanManager.setOPMode(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /*private void initIData() {
        //该系统级服务是盈达设备专有，获取此服务判空避免使用非盈达设备闪退
        @SuppressLint("WrongConstant") Object service = getSystemService("idata");
        if (null != service) {
            iScanInterface miScanInterface = new iScanInterface(this);
            *//*配置扫描结果输出方式
             * mode  0：焦点输出   （没有焦点的时候会误触发UI）
             *       1：广播输出    action：android.intent.action.SCANRESULT
             *       2：模拟按键输出   （没有焦点的时候会误触发UI）
             *       3：复制到粘贴板
             *//*
            miScanInterface.setOutputMode(1);
        }
    }*/
}
